<template>
  <BottomDrawer
    :visible="addVisible"
  
    title="复制排班"
    :width="538"
    @close="addVisible = false"
  >
    <div style="padding: 0px;">
    
      <PageForm :gutter="'0'"
        ref="pageForm"
        :labelPosition="'left'"
        v-if="addVisible"
        :list="list"
        :form-data="formData"
      >
      </PageForm>
      <errorList ref="errorListRef"></errorList>
      
    </div>

    <template #footer>
      <el-button @click="handleSubmit" type="primary">确 定</el-button>
      <el-button @click="addVisible = false">取 消</el-button>
    </template>
  </BottomDrawer>
</template>

<script setup lang="ts">

import type { FormRules, ElTree } from 'element-plus';
import type { CopyRegisration, ShiftRole} from '@/types/registrationSettings/index';
import { copySchedule} from '@/api/register/index';
import { deptGetSelectList } from '@/api/account'
import errorList from './errorList.vue';
const errorListRef = ref<InstanceType<typeof errorList>>();
const rules = reactive<FormRules<CopyRegisration>>({
    deptIdList: [
        { required: true, message: '请选择科室', trigger: 'blur' },
    ],
    fromStart: [
        { required: true, message: '请输入开始时间', trigger: 'blur' },
    ],
    fromEnd: [
        { required: true, message: '请输入结束时间', trigger: 'blur' },
    ],
    targetStart:[
        { required: true, message: '请输入目标开始时间', trigger: 'blur' },
    ],
    targetEnd:[
        { required: true, message: '请输入目标结束时间', trigger: 'blur' },

    ]
});
const defaultProps = {
  children: 'children',
  label: 'name',
};
// 获取序列列表
const pageForm = ref();

const filterText = ref('');
const treeRef = ref<InstanceType<typeof ElTree>>();
watch(filterText, (val) => {
  treeRef.value!.filter(val);
});
const list = ref<any>();
defineExpose<{
  open: () => void;
}>({
  open: async() => {
  formData.value = {
     deptIdList:[],
    fromStart:'',
    fromEnd:'',
    targetStart:'',
    targetEnd:''
  }
  await getDeptGetSelectList()
  list.value = [
    {
      list: [
        {
          span: 24,
          label: '将',
          labelR: '的排班计划',
          labelRNum:'12',
          labelWidth:'100px',
          prop: 'deptIdList',
          customize:true,
          componentProps:{
              placeholder:'科室',
              width:'306'
          },
          multiple:true,
          selectGroupOptions:deptList,
          component: 'select',
        },
        {
          span: 14,
          labelWidth:'100px',
          component: 'date',
          prop: 'fromStart',
          label: '从当前周期',
          componentProps:{
            width:'192px',
            placeholder:'起始日期'
          },
        },
        {
          span: 10,
          labelWidth:'1px',
          labelLNum:'12',
          component: 'date',
          prop: 'fromEnd',
          label: '',
          componentProps:{
            width:'192px',
            placeholder:'终止时间'
          },
        },

  {
          span: 14,
          component: 'date',
          labelWidth:'100px',
          prop: 'targetStart',
          label: '复制到',
          componentProps:{
            width:'192px',

            placeholder:'起始日期'
          },
        },
        {
          span: 10,
          labelWidth:'1px',
          component: 'date',
          labelLNum:'12',
          prop: 'targetEnd',
          label: '',
          componentProps:{
            width:'192px',
            placeholder:'终止时间'
          },
        },

      ],
    },
  ]


  addVisible.value = true;
  },
});

const addVisible = ref(false);

const formData = ref<CopyRegisration>({
  deptIdList:[],
  fromStart:'',
  fromEnd:'',
  targetStart:'',
  targetEnd:''
});
const deptList = ref<any>([])
const getDeptGetSelectList = async () => {
  deptList.value = []
  let res = await deptGetSelectList()
  if(res.code !== '200') {
    //ElMessage.error(res.msg);
    return;
  }
  res.data.forEach(element => {
     deptList.value.push({
      label:element.deptName,
      value:element.deptId
     })
  });
}




//console.log(list);

const copyScheduleApi = async () => {
    let res = await copySchedule(formData.value)
    if (res.code != '200') {
        return
    }else{
      if(res.data.isCancel && res.data.isCancel == '1'){
        errorListRef.value?.open(res)
      }else{
         ElMessage({
          type: 'success',
          message: '保存成功'
        });
        closeDrawer()
      }
     
    }
};

const handleSubmit = async () => {
  copyScheduleApi()
  
  // const ruleFormRef = pageForm.value?.ruleFormRef;
  // await ruleFormRef.validate((valid: any, fields: any) => {
  //   if (valid) {
  //   } else {
  //     //console.log('error submit!', fields);
  //   }
  // });
};
const emit = defineEmits(['closeDrawer']);
const closeDrawer = () => {
    addVisible.value = false
    emit('closeDrawer');
};
onMounted(() => {
});
</script>
<style lang="scss" scoped>
.tree-box {
  border: 2px solid #e1e1e1;
  height: 300px;
  width: 451px;
  margin-top: 10px;
  overflow-y: auto;
}
</style>
